package logic;

import interfaze.SuperArcade2in1;

import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;
import java.net.URL;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.JButton;

import constants.BonusType;
import constants.PercentNumber;

/** The class that contains the utilities.
 * @author Team 1
 *
 */
public class Utilities {
	
	/** Loads and returns image from the resources.
	 * @param path : the path to the resource.
	 * @return : The image the image loaded.
	 */
	public static Image loadImage(String path) {
		URL url = Utilities.class.getClassLoader().getResource(path);
		Image image = null;
		try {
			image = ImageIO.read(url);
		} catch (IOException e) {
			System.out.println("Error occurred while loading image");
		}
		return image;
	}
	
	/** Adds an effect to a button.
	 * @param button : the button in which the effect will be applied.
	 * @param increase : the increase or decrease pixels number.
	 * @param xScale : The relation between the window's width and the display's width.
	 * @param yScale : The relation between the window's height and the display's height.
	 */
	public static void buttonEffect(JButton button, int increase, float xScale, float yScale) {
		if(increase >= 0) {
			button.setBounds(button.getX() - Math.round(increase * xScale), button.getY() - Math.round(increase * yScale), 
				button.getWidth() + Math.round(2 * increase * xScale), button.getHeight() + Math.round(2 * increase * yScale));
			button.setFont(new Font(button.getFont().getFamily(), button.getFont().getStyle(),
				Math.round(button.getFont().getSize2D() + (increase * xScale))));
		} else {
			button.setBounds(button.getX() + Math.round(-1 * increase * xScale), button.getY() + Math.round(-1 * increase * yScale), 
					button.getWidth() - Math.round(-2 * increase * xScale), button.getHeight() - Math.round(-2 * increase * yScale));
			button.setFont(new Font(button.getFont().getFamily(), button.getFont().getStyle(),
					Math.round(button.getFont().getSize2D() - (-1 * increase * xScale))));
		}
	}
	
	/** Resizes and image to the specified horizontal and vertical proportions.
	 * @param xScale : The relation between the window's width and the display's width.
	 * @param yScale : The relation between the window's height and the display's height.
	 * @param path : The path to the resource.
	 * @return the image rescaled.
	 */
	public static Image resizeImage(float xScale, float yScale, String path) {
		Image image = null;
		try {
			image = ImageIO.read(Utilities.class.getClassLoader().getResource(path));
			image = image.getScaledInstance(Math.round(image.getWidth(null) * xScale),
					Math.round(image.getHeight(null) * yScale), Image.SCALE_SMOOTH);
		} catch(IOException e) {
			e.printStackTrace();
		}
		return image;
	}
	
	/** Parses a resolution to string.
	 */
	public static String resolutionToStringParser(int width, int height) {
		if(width != (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() 
				&& height != (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()) {
					StringBuilder resolution = new StringBuilder();
					resolution.append(width);
					resolution.append('x');
					resolution.append(height);
					return resolution.toString();
		} else {
			return "Full Screen";
		}
	}
	
	/** Resizes the window to the specified resolution.
	 * @param resolution : The resolution given as a String, e.g. 1280x720.
	 * @param window : The JFrame that will be processed.
	 */
	public static void stringToResolutionParser(String resolution, SuperArcade2in1 window) {
		window.setWidth(Integer.parseInt(resolution.substring(0, resolution.indexOf("x"))));
		window.setHeight(Integer.parseInt(resolution.substring(resolution.indexOf("x") + 1, resolution.length())));
		if(window.isOnFullScreen()) {
			window.setOnFullScreen(false);
			window.dispose();
			window.setUndecorated(false);
			window.getDevice().setFullScreenWindow(null);
			window.setVisible(true);
		}
		window.updateXscale();
		window.updateyScale();
	}
	
	/** Sets the window to the full screen mode.
	 * @param window : The window to be processed.
	 */
	public static void windowToFullScreen(SuperArcade2in1 window) {
		window.setWidth(window.getFULLSCREEN_WIDTH());
		window.setHeight(window.getFULLSCREEN_HEIGHT());
		window.setOnFullScreen(true);
		window.dispose();
		window.setUndecorated(true);
		window.getDevice().setFullScreenWindow(window);
		window.setVisible(true);
		window.updateXscale();
		window.updateyScale();
	}
	
	/** Converts the percent 0, 0%; 1, 25%; 2, 50%; 3, 100%
	 * to the numbers that allows that percent with a random.
	 */
	public static int percentToRandomParser(PercentNumber percent) {
		Random random = new Random();
		switch(percent) {
		case ZERO:
			return -1;
		case TWENTY_FIVE:
			return random.nextInt(3);
		case FIFTY:
			return random.nextInt(2);
		case HUNDRED:
			return random.nextInt(1);
		default:
			//Whatever
			return Integer.MAX_VALUE;
		}
	}
	
	/** Generates a random percent.
	 * @return a randomized percent.
	 */
	public static PercentNumber randomPercentNumber() {
		int size = PercentNumber.values().length;
		Random random = new Random();
		return PercentNumber.values()[random.nextInt(size)];
	}
	
	/** Generates a random bonus.
	 * @return a randomized bonus type.
	 */
	public static BonusType randomBonusType() {
		int size = BonusType.values().length;
		Random random = new Random();
		return BonusType.values()[random.nextInt(size)];
	}
	
	/** Parses a character array to String.
	 * @param charArray : the character array input.
	 * @return the char array parsed to string.
	 */
	public static String charArrayToString(char[] charArray) {
		StringBuilder string = new StringBuilder();
		for(int i = 0; i < charArray.length; i++) {
			string.append(charArray[i]);
		}
		return string.toString();
	}
	
	/** Parses integers to booleans.
	 * @param value : the integer value to be processed.
	 * @return true if the value isn't equal to zero, false otherwise.
	 */
	public static Boolean intToBoolean(int value) {
		if(value != 0) {
			return true;
		} else {
			return false;
		}
	}
}